package appConfig

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

type Mysql struct {
	User     string
	Passwd   string
	Host     string
	Port     int64
	Database string
}

type Redis struct {
	Addr   string
	Passwd string
}

type Upload struct {
	Server struct {
		Upload struct {
			Mode  string `yaml:"mode"`
			Local struct {
				Path string `yaml:"path"`
			} `yaml:"local"`
			Oss struct {
				Endpoint        string `yaml:"endpoint"`
				AccessKeyId     string `yaml:"access_key_id"`
				AccessKeySecret string `yaml:"access_key_secret"`
				BucketName      string `yaml:"bucket_name"`
				Path            string `yaml:"path"`
			} `yaml:"oss"`
			Qiniu struct {
				AccessKey string `yaml:"access_key"`
				SecretKey string `yaml:"secret_key"`
				Bucket    string `yaml:"bucket"`
				Domain    string `yaml:"domain"`
				Path      string `yaml:"path"`
			} `yaml:"qiniu"`
		} `yaml:"upload"`
	} `yaml:"server"`
}

type AppConfig struct {
	Mysql
	Redis
	Upload
}

var ConfigData AppConfig

func Init() {
	viper.SetConfigFile("D:\\gowork\\src\\yk\\zg3_yk\\src\\pro\\appConfig\\config.yaml")
	viper.ReadInConfig()
	viper.Unmarshal(&ConfigData)
	fmt.Println(ConfigData)

	viper.WatchConfig()
	viper.OnConfigChange(func(e fsnotify.Event) {
		err := viper.Unmarshal(&ConfigData)
		if err != nil {
			return
		}
		fmt.Println("配置文件跟新成功", ConfigData)
	})
}
